//
//  BSStatus.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/29.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

//https://api.weibo.com/2/statuses/home_timeline.json?access_token=2.00YteGpB3bO98B4c9e9a467352OiJD

//微博模型
class BSStatus: NSObject {

    ///微博ID
    var id: Int64 = 0
    
    ///微博创建时间
    var created_at:	String?
    
    ///微博信息内容
    var text: String?
    
    ///微博来源
    var source:	String?
    
    ///微博配图-字典数组
    var pic_urls: [[String: AnyObject]]?{
        didSet{
            if pic_urls?.count == 0 {
                return
            }
            
            //数组中有字典
            //创建数组保存URL
            storePictureURLs = [NSURL]()
            
            for dict in pic_urls!{
                let urlString = dict["thumbnail_pic"] as? String
                
                let url = NSURL(string: urlString!)
                storePictureURLs?.append(url!)
            }
        }
    }
    
    ///直接存放图片的URL
    var storePictureURLs: [NSURL]?
    
    ///转发数
    var reposts_count: Int = 0
    
    ///评论数
    var comments_count:	Int = 0
    
    ///表态数
    var attitudes_count: Int = 0
    
    ///用户模型
    var user: BSUser?
    
    ///被转发微博模型,不一定有
    var retweeted_status: BSStatus?
    
    /*****************************/
     
    ///构造函数
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写setValueForKey
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            //自己来字典转模型,不要系统帮我们设置字典,因为key(user)的value值是一个字典
            //将value转成字典
            if let dict = value as? [String: AnyObject]{
                //转模型
                user = BSUser(dict: dict)
            }
            return
        }
        
        //被转发微博
        if key == "retweeted_status" {
            //自己来字典转模型
            if let dict = value as? [String: AnyObject]{
                //转模型
                retweeted_status = BSStatus(dict: dict)
            }
            return 
        }
        
        //其他ket走正常 setValue:forKey:
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    ///对象打印
    override var description: String{
        get{
            let keys = ["id", "created_at", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count"]
            
            //根据传入的keys,分别拿每一个key去模型找找对应的value,找到了值之后,用key和value拼接成字典
            return dictionaryWithValuesForKeys(keys).description
        }
    }
}
